/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test7.problem1;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;

/**
 *
 * @author hoang.pt
 */
public class Demo1 {

    public Demo1() {
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        //Collection String
        LinkedList<String> list = new LinkedList<String>();

        //input 4
        for (int i = 1; i <= 4; i++) {
            System.out.println("Student " +i+ ":");
            System.out.println("Name: ");
            list.add(in.readLine());
        }

        //read list
        for (String x : list) {
            System.out.print(x + " ");
        }
        System.out.print("\n");

        //write to file
        ObjectOutputStream writer = new ObjectOutputStream(new FileOutputStream("student.txt"));
        writer.writeObject(list);
        writer.flush();
        writer.close();

        //read from file
        ObjectInputStream reader = new ObjectInputStream(new FileInputStream("student.txt"));
        LinkedList<String> list2 = (LinkedList<String>) reader.readObject();
        System.out.println("--------read from file-----");

        for (int i=0; i< list2.size(); i++) {
            System.out.println(list2.get(i));
        }
    }
}
